package com.dhcc.sds.standard.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.cache.rulecal.IRuleCalCache;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.standard.entity.SdsStandRuleBaseCalEntity;
import com.dhcc.sds.standard.entity.SdsStandRuleBaseEntity;
import com.dhcc.sds.standard.service.ISdsStandDataSourceService;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import com.dhcc.sds.standard.service.ISdsStandRuleBaseService;
import com.dhcc.sds.standard.service.IsdsStandRuleCalService;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 规则库
 *
 * @author lyh
 * @ClassName: SdsStandRuleBaseController
 * @Description: TODO
 */

@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstandrulebase")
public class SdsStandRuleBaseController {

    private String PREFIX = "/backend/sds/sdsstandrulebase/";

    @Autowired
    private ISdsStandRuleBaseService standRuleBaseService;
    @Autowired
    private ISdsStandDataSourceService standDataSourceService;
    @Autowired
    private ISdsStandDicService sdsStandDicService;
    @Autowired
    private IsdsStandRuleCalService standRuleCalService;
    @Autowired
    private ISdsCommonService sdsCommonService;

    /**
     * 跳转到规则库维护页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsstandrulebase.html";
    }


    /**
     * 获取数据源列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false, defaultValue = "0") Map<String, Object> params) {
        Page<SdsStandRuleBaseEntity> page = new PageFactory<SdsStandRuleBaseEntity>().defaultPage();
        List<SdsStandRuleBaseEntity> result = this.standRuleBaseService.page(page, params);
        page.setRecords((List<SdsStandRuleBaseEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 跳转新增规则
     */
    @RequestMapping(value = "/to_add")
    public String to_add(Model model) {
        JSONArray dicDescMap = sdsStandDicService.getDicDescMap();
        JSONArray dataDescMap = standDataSourceService.getDataDescMap();
        JSONArray valueTimeMap = sdsCommonService.getDicArray("ItemGetValTime");
        JSONObject ruleTypeMap = sdsCommonService.getDicMap("SDQCRuleBaseType");
        String maxCode = standRuleBaseService.getMaxCode();
        int nextCode = Integer.parseInt(maxCode)+1;
        model.addAttribute("ruleTypeMap",ruleTypeMap);
        model.addAttribute("dicDescMap", dicDescMap);
        model.addAttribute("dataDescMap", dataDescMap);
        model.addAttribute("valueTimeMap",valueTimeMap);
        model.addAttribute("nextCode",nextCode);
        return PREFIX + "sdsstandrulebase_add.html";
    }

    /**
     * 新增规则
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsStandRuleBaseEntity sdsStandRuleBase) {
        if (CommonUtil.isOneEmpty(sdsStandRuleBase)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        standRuleBaseService.insert(sdsStandRuleBase);
        return R.ok(sdsStandRuleBase.getId().toString());
    }

    /**
     * 获取数据源
     */
    @RequestMapping(value = "/getDataMap")
    @ResponseBody
    public JSONArray getDataMap(Model model) {
        JSONArray dataSouceMap = standDataSourceService.getDataDescMap();
        model.addAttribute("dataSouceMap", dataSouceMap);
        return dataSouceMap;
    }

    /**
     * 获取数据源
     */
    @RequestMapping(value = "/getDicMap")
    @ResponseBody
    public JSONObject getDicMap(Model model) {
        JSONObject dicMap = sdsStandDicService.getDicMap();
        model.addAttribute("dicMap", dicMap);
        return dicMap;
    }

    /**
     * 跳转修改规则页面
     */
    @RequestMapping(value = "/to_update/{id}")
    public String toUpdate(@PathVariable("id") Long sdsStandRuleBaseId, Model model) {
        SdsStandRuleBaseEntity sdsStandRuleBase = standRuleBaseService.selectById(sdsStandRuleBaseId);
        JSONArray dicDescMap = sdsStandDicService.getDicDescMap();
        JSONArray dataDescMap = standDataSourceService.getDataDescMap();
        JSONArray valueTimeMap = sdsCommonService.getDicArray("ItemGetValTime");
        JSONObject ruleTypeMap = sdsCommonService.getDicMap("SDQCRuleBaseType");
        model.addAttribute("ruleTypeMap",ruleTypeMap);
        model.addAttribute("dicDescMap", dicDescMap);
        model.addAttribute("dataDescMap", dataDescMap);
        model.addAttribute("sdsStandRuleBase", sdsStandRuleBase);
        model.addAttribute("valueTimeMap",valueTimeMap);
        return PREFIX + "sdsstandrulebase_edit.html";
    }

    /**
     * 修改规则
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsStandRuleBaseEntity sdsStandRuleBase) {
        if (CommonUtil.isOneEmpty(sdsStandRuleBase)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsStandRuleBaseEntity old = standRuleBaseService.selectById(sdsStandRuleBase.getId());

        old.setRuleCode(sdsStandRuleBase.getRuleCode());
        old.setRuleDesc(sdsStandRuleBase.getRuleDesc());
        old.setRuleType(sdsStandRuleBase.getRuleType());
        old.setComment(sdsStandRuleBase.getComment());
        old.setIsDefault(sdsStandRuleBase.getIsDefault());
        old.setCalType(sdsStandRuleBase.getCalType());
        standRuleBaseService.updateAllColumnById(old);
        return R.ok();
    }

    /**
     * 删除数据源定义
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long SdsRuleBaseId) {

        this.standRuleBaseService.deleteById(SdsRuleBaseId);

        return R.ok();
    }

    /**
     * 新增规则运算
     */
    @RequestMapping("/addCal")
    @ResponseBody
    @CacheClear(IRuleCalCache.CACHE_NAME)
    public Object addRule(SdsStandRuleBaseCalEntity sdsStandRuleBaseCal) {
        if (CommonUtil.isOneEmpty(sdsStandRuleBaseCal)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        String s = StringEscapeUtils.unescapeHtml(sdsStandRuleBaseCal.getCalculate());
        sdsStandRuleBaseCal.setCalculate(s);
        sdsStandRuleBaseCal.insert();
        return R.ok();
    }

    /**
     * 更新规则运算
     */
    @RequestMapping("/updateCal")
    @ResponseBody
    @CacheClear(IRuleCalCache.CACHE_NAME)
    public Object updateCal(SdsStandRuleBaseCalEntity sdsStandRuleBaseCal) {
        if (CommonUtil.isOneEmpty(sdsStandRuleBaseCal)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsStandRuleBaseCalEntity old = this.standRuleCalService.selectById(sdsStandRuleBaseCal.getId());
        old.setCurrentTime(sdsStandRuleBaseCal.getCurrentTime());
        old.setDatasource(sdsStandRuleBaseCal.getDatasource());
        old.setDic(sdsStandRuleBaseCal.getDic());
        old.setValueTime(sdsStandRuleBaseCal.getValueTime());
        String s = StringEscapeUtils.unescapeHtml(sdsStandRuleBaseCal.getCalculate());
        old.setCalculate(s);
        old.setPriority(sdsStandRuleBaseCal.getPriority());
        standRuleCalService.updateAllColumnById(old);
        return R.ok();
    }

    /**
     * 加载运算规则
     */
    @RequestMapping("/listCal/{ruleId}")
    @ResponseBody
    public Object listCal(@PathVariable Long ruleId, @RequestParam Map<String, Object> params) {
        Page<SdsStandRuleBaseCalEntity> page = new PageFactory<SdsStandRuleBaseCalEntity>().dataGridPage();
        params.put("ruleId", ruleId);
        List<SdsStandRuleBaseCalEntity> result = this.standRuleCalService.page(page, params);
        page.setRecords((List<SdsStandRuleBaseCalEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping("/delCal")
    @ResponseBody
    @CacheClear(IRuleCalCache.CACHE_NAME)
    public Object delCal(@RequestParam Long SdsRuleCalId) {
        this.standRuleCalService.deleteById(SdsRuleCalId);
        return R.ok();
    }
}


